package com.wzh.controller;

/**
 * @description:
 * @author: WZH
 * @time: 2021/6/10 15:55
 */

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wzh.dao.StuMapper;
import com.wzh.db.Stu;
import org.junit.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 测试连接Mybatis-plus
 */
@RestController
@RequestMapping("/my/api")
public class TestMyController {

    //如果出现红色下划线不影响运行，可以设置idea不报红
    @Autowired
    private StuMapper stuMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    /**
     * 查询所有用户信息
     */
    @GetMapping("/list")
    public JSONArray test(){

        //直接调用BaseMapper封装好的CRUD方法，就可实现无条件查询数据
        List<Stu> list = stuMapper.selectList(null);

        //循环获取用户数据
        for (Stu stu:list){
            //获取用户名称
            System.out.println(stu.getSname());
            System.out.println(stu.getSid());
        }
        return JSONUtil.parseArray(list);
    }


    /**
     * 查询一个用户信息
     */
    @GetMapping("/getById/{id}")
    @Cacheable(cacheNames = "names" ,key = "#id")
    public JSONObject test2(@PathVariable("id") Integer id){
        Stu stu = stuMapper.selectById(id);
        return JSONUtil.parseObj(stu);
    }

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "ok";
    }

    @RequestMapping(value = "/cc/{name:.+}", method = RequestMethod.GET)
    public JSONObject cc(@PathVariable String name){

        System.out.println(name);
        return null;
    }




}
